package com.dabaozhan.modules.wx_app.controller;

import com.alibaba.fastjson.JSON;
import com.dabaozhan.common.exception.RRException;
import com.dabaozhan.common.utils.RandomUtils;
import com.dabaozhan.modules.wx_app.base.ResponseDto;
import com.dabaozhan.modules.wx_app.entity.SiteCategoryEntity;
import com.dabaozhan.modules.wx_app.entity.SiteOrderEntity;
import com.dabaozhan.modules.wx_app.entity.SysCategoryEntity;
import com.dabaozhan.modules.wx_app.enums.ResponseCodeEnum;
import com.dabaozhan.modules.wx_app.service.SiteCategoryService;
import com.dabaozhan.modules.wx_app.service.SiteOrderService;
import com.dabaozhan.modules.wx_app.service.SysCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author houzhong
 * @date 2019-01-09 16:20
 */

@Api(value = "用户类目", description = "用户类目")
@RestController
@RequestMapping("/wx_app")
public class UserCategoryController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserCategoryController.class);

    @Autowired
    private SysCategoryService sysCategoryService;

    @Autowired
    private SiteCategoryService siteCategoryService;

    @Autowired
    private SiteOrderService siteOrderService;

    @ApiOperation(value = "查询当前用户所有分类")
    @RequestMapping(value = "/getListCategory")
    public String getListCategory(String userId) {
        ResponseDto<List<SysCategoryEntity>> responseDto = new ResponseDto<List<SysCategoryEntity>>();
        try {
            List<SysCategoryEntity> listSysCategory = sysCategoryService.findListSysCategory();
            if (listSysCategory.size() > 0) {
                for (SysCategoryEntity categoryEntity : listSysCategory ) {
                    SiteCategoryEntity siteCategory = new SiteCategoryEntity();
                    siteCategory.setUserId(userId);
                    siteCategory.setParentId(categoryEntity.getCategoryId());
                    List<SiteCategoryEntity> list = siteCategoryService.findBySiteCategoryList(siteCategory);
                    if (list.size()>0) {
                        categoryEntity.setCategoryList(list);
                    }
                }
            }
            responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
            responseDto.setResult(listSysCategory);
        } catch(RRException re){
            responseDto.setCode(re.getCode());
            responseDto.setMessage(re.getMessage());
        } catch (Exception e) {
            responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
            //记录异常日志
            LOGGER.error("查询当前用户所有分类,异常详情如下:",e);
        } finally {
            //记录请求入参和返回日志
            LOGGER.info("查询当前用户所有分类请求参数:{}-返回结果:{}", JSON.toJSONString(userId),JSON.toJSONString(responseDto));
        }
        return JSON.toJSONString(responseDto);
    }

    @ApiOperation(value = "新增分类")
    @RequestMapping(value = "/saveSiteCategory")
    public String saveSiteCategory(SiteCategoryEntity v) {
        ResponseDto<SiteCategoryEntity> responseDto = new ResponseDto<SiteCategoryEntity>();
        try {
            //生成随机类目编码
            v.setCategoryId(RandomUtils.randomStr());
            v.setStatus(1);
            siteCategoryService.saveSiteCategoey(v);

            responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
        } catch(RRException re){
            responseDto.setCode(re.getCode());
            responseDto.setMessage(re.getMessage());
        } catch (Exception e) {
            responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
            //记录异常日志
            LOGGER.error("当前用户新增分类,异常详情如下:",e);
        } finally {
            //记录请求入参和返回日志
            LOGGER.info("当前用户新增分类请求参数:{}-返回结果:{}", JSON.toJSONString(v),JSON.toJSONString(responseDto));
        }
        return JSON.toJSONString(responseDto);
    }

    @ApiOperation(value = "当前用户修改分类")
    @RequestMapping(value = "/updateSiteCategory")
    public String updateSiteCategory(SiteCategoryEntity v) {
        ResponseDto<SiteCategoryEntity> responseDto = new ResponseDto<SiteCategoryEntity>();
        try {
            siteCategoryService.updateSiteCategoey(v);
            responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
        } catch(RRException re){
            responseDto.setCode(re.getCode());
            responseDto.setMessage(re.getMessage());
        } catch (Exception e) {
            responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
            //记录异常日志
            LOGGER.error("当前用户修改分类,异常详情如下:",e);
        } finally {
            //记录请求入参和返回日志
            LOGGER.info("当前用户修改分类请求参数:{}-返回结果:{}", JSON.toJSONString(v),JSON.toJSONString(responseDto));
        }
        return JSON.toJSONString(responseDto);
    }

    @ApiOperation(value = "删除分类")
    @RequestMapping(value = "/deleteSiteCategory")
    public String deleteSiteCategory(SiteCategoryEntity v) {
        ResponseDto<SiteCategoryEntity> responseDto = new ResponseDto<SiteCategoryEntity>();
        try {
            List<SiteOrderEntity> listOrder = siteOrderService.getSiteOrderCategoryId(v.getUserId(),v.getCategoryId(),v.getParentId());
            if (listOrder.size()>0) {
                responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_601);
            }else {
                siteCategoryService.deleteSiteCategory(v.getId());
                responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
            }
        } catch(RRException re){
            responseDto.setCode(re.getCode());
            responseDto.setMessage(re.getMessage());
        } catch (Exception e) {
            responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
            //记录异常日志
            LOGGER.error("删除分类,异常详情如下:",e);
        } finally {
            //记录请求入参和返回日志
            LOGGER.info("删除分类请求参数:{}-返回结果:{}", JSON.toJSONString(v),JSON.toJSONString(responseDto));
        }
        return JSON.toJSONString(responseDto);
    }

    @ApiOperation(value = "根据id查询")
    @RequestMapping(value = "/getSiteCategoryId")
    public String getSiteCategoryId(String id) {
        ResponseDto<SiteCategoryEntity> responseDto = new ResponseDto<SiteCategoryEntity>();
        try {
            SiteCategoryEntity v = siteCategoryService.getSiteCategoryId(id);
            responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
            responseDto.setResult(v);
        } catch(RRException re){
            responseDto.setCode(re.getCode());
            responseDto.setMessage(re.getMessage());
        } catch (Exception e) {
            responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
            //记录异常日志
            LOGGER.error("根据id查询,异常详情如下:",e);
        } finally {
            //记录请求入参和返回日志
            LOGGER.info("根据id查询请求参数:{}-返回结果:{}", JSON.toJSONString(responseDto));
        }
        return JSON.toJSONString(responseDto);
    }


}
